<?php
session_start();
if (!isset($_SESSION['user_id'])) {
    header('Location: login21.php');
    exit;
}

require_once 'class21.php';

$error = '';
$success = '';
$classModel = new Classes();

// 获取要编辑的班级ID
$id = $_GET['id'] ?? null;
if (!$id) {
    header('Location: classes21.php');
    exit;
}

// 获取班级信息
$class = $classModel->getClass($id);
if (!$class) {
    header('Location: classes21.php');
    exit;
}

// 获取所有系部
$departments = $classModel->getDepartments();

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $name = $_POST['class_name'] ?? '';
    $department_id = $_POST['department_id'] ?? '';
    
    if (empty($name)) {
        $error = '班级名称不能为空';
    } elseif (empty($department_id)) {
        $error = '请选择所属系部';
    } else {
        try {
            $classModel->updateClass($id, $name, $department_id);
            header('Location: classes21.php?success=班级修改成功');
            exit;
        } catch (Exception $e) {
            $error = $e->getMessage();
        }
    }
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>教学服务系统 - 修改班级</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <div class="header">
        <div class="container">
            <h1>教学服务系统</h1>
        </div>
    </div>

    <div class="main-nav">
        <div class="container">
            <ul>
                <li><a href="index.php">主页</a></li>
                <li><a href="departments21.php">系部管理</a></li>
                <li><a href="classes21.php">班级管理</a></li>
                <li><a href="students21.php">学生管理</a></li>
                <li><a href="teachers21.php">教师管理</a></li>
                <li><a href="courses21.php">课程管理</a></li>
                <li><a href="scores21.php">成绩管理</a></li>
                <li><a href="teachings21.php">授课管理</a></li>
                <li class="logout"><a href="logout21.php">退出登录</a></li>
            </ul>
        </div>
    </div>

    <div class="container">
        <div class="card">
            <div class="card-header">
                <h2 class="card-title">修改班级</h2>
            </div>
            
            <?php if ($error): ?>
            <div class="message message-error"><?php echo $error; ?></div>
            <?php endif; ?>
            
            <div class="card-body">
                <form method="post">
                    <div class="form-group">
                        <label for="class_name">班级名称</label>
                        <input type="text" id="class_name" name="class_name" required
                               class="form-control" value="<?php echo htmlspecialchars($class['class_name']); ?>" maxlength="100">
                    </div>
                    
                    <div class="form-group">
                        <label for="department_id">所属系部</label>
                        <select id="department_id" name="department_id" required class="form-control">
                            <option value="">请选择系部</option>
                            <?php foreach ($departments as $dept): ?>
                            <option value="<?php echo $dept['department_id']; ?>" 
                                <?php echo ($dept['department_id'] == $class['department_id']) ? 'selected' : ''; ?>>
                                <?php echo $dept['department_name']; ?>
                            </option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    
                    <div class="form-actions">
                        <button type="submit" class="btn btn-primary">保存</button>
                        <a href="classes21.php" class="btn btn-secondary">取消</a>
                    </div>
                </form>
            </div>
        </div>
    </div>
</body>
</html>